package org.kealinghornets.jchuah.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import java.util.Random;

//REMEMBER FOR NEXT TIME : CHANGE THE OTHERCLOCK VARIABLE
public class MyActivity2 extends Activity {

    int numCleared = 0;
    int getRow = 0;
    int getColumn = 0;
    public void onColorButtonPush(final View myView) {
        Log.d("kealinghornets.myApp", "The Button Pushed");
        Toast.makeText(this, "DUCKIEES!!!", Toast.LENGTH_SHORT).show();
        //Animation Fade Out for Button
        Animation fade_out_animation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
        fade_out_animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

                myView.setEnabled(false);
            }


            @Override
            public void onAnimationEnd(Animation animation) {
                myView.setEnabled(true);
                myView.setAlpha(0);
                myView.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //Animation Fade In for button
        Animation fade_in_animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        fade_in_animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                myView.setAlpha(0);
                myView.setEnabled(false);

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                myView.setEnabled(true);
                myView.setAlpha(1);
                myView.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //Defining more animations
        Animation otherFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        Animation otherFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
        Animation otherFadeInRight = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        Animation otherFadeOutRight = AnimationUtils.loadAnimation(this, R.anim.fade_out);


        String buttonName = getResources().getResourceEntryName(myView.getId());
        int buttonRow = Integer.parseInt(buttonName.charAt(6) + "");
        int buttonColumn = Integer.parseInt(buttonName.charAt(7) + "");
        Log.d("clock", "row: " + buttonRow + "column: " + buttonColumn);

        if (myView.getAlpha() == 1) {
            myView.startAnimation(fade_out_animation);
            numCleared = numCleared + 1;

        } else {
            myView.startAnimation(fade_in_animation);
            numCleared = numCleared - 1;
        }


        getRow = buttonRow + 1;
        getColumn = buttonColumn;

        int getRowUp = buttonRow;
        int getColumnUp = buttonColumn + 1;

        final View otherClock = ((View) myView.getParent()).findViewById(getResources().getIdentifier("button" + getRow + getColumn, "id", getPackageName()));
        final View otherClockRight = ((View) myView.getParent()).findViewById(getResources().getIdentifier("button" + getRowUp + getColumnUp, "id", getPackageName()));
//Not making it crash
        if (buttonRow != 4)
            if (otherClock.getAlpha() == 1) {
                otherClock.startAnimation(otherFadeOut);
                numCleared = numCleared + 1;
            } else if (otherClock.getAlpha() == 0) {
                otherClock.startAnimation(otherFadeIn);
                numCleared = numCleared - 1;
            }

        if (buttonColumn != 3)
            if (otherClockRight.getAlpha() == 1) {
                otherClockRight.startAnimation(otherFadeOutRight);
                numCleared = numCleared + 1;
            } else if (otherClockRight.getAlpha() == 0) {
                otherClockRight.startAnimation(otherFadeInRight);
                numCleared = numCleared - 1;
            }


        otherFadeIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                otherClock.setAlpha(1);
                otherClock.setEnabled(false);

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                otherClock.setEnabled(true);
                otherClock.setAlpha(1);
                otherClock.clearAnimation();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        otherFadeInRight.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                otherClockRight.setAlpha(1);
                otherClockRight.setEnabled(false);

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                otherClockRight.setEnabled(true);
                otherClockRight.setAlpha(1);
                otherClockRight.clearAnimation();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });



        otherFadeOut.setAnimationListener((new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                otherClock.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                otherClock.setEnabled(true);
                otherClock.setAlpha(0);
                otherClock.clearAnimation();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        }));

        otherFadeOutRight.setAnimationListener((new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                otherClockRight.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                otherClockRight.setEnabled(true);
                otherClockRight.setAlpha(0);
                otherClockRight.clearAnimation();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        }));

        if (numCleared == 20){
            startActivity(new Intent(this, MyActivity3.class) );
        }


    }







    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_activity2);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
